#include "objectivechooseform.h"
#include "ui_objectivechooseform.h"
#include <QPainter>
#include <QStyleOption>

ObjectiveChooseForm::ObjectiveChooseForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ObjectiveChooseForm)
{
    ui->setupUi(this);
    this->setProperty("form","backgroundform");
    
    // 不需要额外的连接，因为 on_comboBox_chooseMode_currentIndexChanged 会通过 Qt 的自动连接机制工作
}

ObjectiveChooseForm::~ObjectiveChooseForm()
{
    delete ui;
}

void ObjectiveChooseForm::SetCalibrationCorrection(bool _state)
{
    ui->comboBox_chooseMode->setHidden(_state);
}

QString ObjectiveChooseForm::getCurrentText() const
{
    return ui->comboBox_chooseMode->currentText();
}

void ObjectiveChooseForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QWidget::paintEvent(event);
}

void ObjectiveChooseForm::setChooseMode(const QString &text)
{
    // 临时断开信号连接，避免无限循环
    QObject::disconnect(ui->comboBox_chooseMode, &QComboBox::currentTextChanged, this, &ObjectiveChooseForm::on_comboBox_chooseMode_currentIndexChanged);
    
    // 查找并设置匹配的文本，使用宽松匹配方式
    int index = ui->comboBox_chooseMode->findText(text, Qt::MatchContains);
    if (index >= 0) {
        ui->comboBox_chooseMode->setCurrentIndex(index);
    }
    
    // 重新连接信号
    QObject::connect(ui->comboBox_chooseMode, &QComboBox::currentTextChanged, this, &ObjectiveChooseForm::on_comboBox_chooseMode_currentIndexChanged);
}

void ObjectiveChooseForm::on_comboBox_chooseMode_currentIndexChanged(const QString &arg1)
{
    emit chooseModeChanged(arg1);
}
